from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404
import urllib
import urllib2

from mindMap.models import *

def test(request):
    """
    Make a couple of test calls to the MindMeister REST service
    to make sure all is well
    """
    key = get_object_or_404(Key, application="MindMeister")
    url = urllib.basejoin('http://www.mindmeister.com/services/rest/', '?method=mm.test.echo&api_key=%s&name=value' % key.apiPublicKey)
    print("Using URL" + url)
    req = urllib2.urlopen(url)
    rdata = []
    chunk = 'xx'
    while chunk:
        chunk = req.read()
        if chunk:
            rdata.append(chunk)
    print rdata
    return HttpResponseRedirect(request.META.get('HTTP_REFERER', ''))